package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.MarketComment;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.WxCommentCountVo;
import com.cskaoyan.bean.vo.WxCommentVo;
import com.cskaoyan.service.WxCommentService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.security.Security;

/**
 * @author yi
 * @date 2022/10/14 16:11
 */

@RestController
@RequestMapping("wx/comment")
public class WxCommentController {
    @Autowired
    WxCommentService wxCommentService;

    @RequestMapping("list")
    public BaseRespVo list(Integer valueId, Byte type, Integer showType, BasePageInfo basePageInfo) {
        CommonData<WxCommentVo> data = wxCommentService.queryComment(valueId, type, showType, basePageInfo);
        return BaseRespVo.ok(data, "成功");
    }

    @RequestMapping("count")
    public BaseRespVo count(Integer valueId, Byte type) {
        WxCommentCountVo wxCommentCountVo = wxCommentService.queryCommentCount(valueId, type);
        return BaseRespVo.ok(wxCommentCountVo);
    }

    @RequestMapping("post")
    public BaseRespVo post(@RequestBody MarketComment marketComment) {

        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        if (principals != null) {
            MarketUser user = (MarketUser) principals.getPrimaryPrincipal();
            Integer userId = user.getId();
            MarketComment marketComment1 = wxCommentService.insertComment(marketComment, userId);
            return BaseRespVo.ok(marketComment1, "成功");
        } else {
            return BaseRespVo.error("请登录");
        }
    }
}
